package com.yzt.message.custom;

import android.os.Parcel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yzt.zhuju.rongyun.IMMessageType;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

import io.rong.common.ParcelUtils;
import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;

/**
 * author : bob
 * date   : 2022/11/24
 * desc   : 骰子的自定义消息
 */
@MessageTag(value = IMMessageType.DICE_MESSAGE, flag = MessageTag.ISPERSISTED | MessageTag.ISCOUNTED)
public class RXDiceMessageContent extends MessageContent {

    private String content;

    public static RXDiceMessageContent setDiceMessageContent(String content) {
        RXDiceMessageContent mRXMessageContent = new RXDiceMessageContent();
        mRXMessageContent.content = content;
        return mRXMessageContent;
    }

    @Override
    public byte[] encode() {
        JSONObject object = new JSONObject();
        object.put("content", content);
        return object.toString().getBytes(StandardCharsets.UTF_8);

    }

    public RXDiceMessageContent() {
    }

    public RXDiceMessageContent(byte[] data) {
        super(data);
        String jsonStr = null;
        try {
            jsonStr = new String(data, StandardCharsets.UTF_8);
            JSONObject object = JSON.parseObject(jsonStr);
            setContent(object.getString("content"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        ParcelUtils.writeToParcel(dest, content);
    }

    public static final Creator<RXDiceMessageContent> CREATOR = new Creator<RXDiceMessageContent>() {

        @Override
        public RXDiceMessageContent createFromParcel(Parcel source) {
            return new RXDiceMessageContent(source);
        }

        @Override
        public RXDiceMessageContent[] newArray(int size) {
            return new RXDiceMessageContent[size];
        }
    };

    public RXDiceMessageContent(Parcel parcel) {
        content = ParcelUtils.readFromParcel(parcel);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}
